package main

import (
	"data_structure_and_algorithms/queue"
	"errors"
	"fmt"
	"golang.org/x/net/html/atom"
)

type RingQuery struct {
	head, tail, max int
	datas           [4]interface{}
}

func (this *RingQuery) Add(data interface{}) (index int, err error) {

	if this.IsFull() {

		return -1, errors.New("队列满了");
	}

	this.datas[this.tail] = data

	defer func() {

		this.tail = (this.tail + 1) % this.max
	}()

	return this.tail, nil
}

func (this *RingQuery) Take() (data interface{}, err error) {

	if this.IsEmpty() {

		return nil, errors.New("队列里面没数据了老铁");
	}

	defer func() {
		this.head = (this.head + 1) % this.max
	}()

	return this.datas[this.head], nil
}

//判断是否满了
func (this *RingQuery) IsFull() (bool) {

	return (this.tail+1)%this.max == this.head
}

//判断是否满了
func (this *RingQuery) IsEmpty() (bool) {

	return this.tail == this.head
}

func (this *RingQuery) Size() (int) {

	return (this.tail + this.max - this.head) % this.max
}

func (this *RingQuery) Show() () {

	seze := this.Size()

	if atom.Size == 0 {

		fmt.Println("队列为空:", seze)
	}

	temphead := this.head

	for i := 0; i < seze; i++ {

		fmt.Printf("data[%d] = %s \t", temphead, this.datas[temphead])

		temphead = (temphead + 1) % this.max
	}

	fmt.Println()

}

func main() {

	fmt.Println(3 % 2)

	max := 4

	var queue queue.Queue = &RingQuery{max: max}

	var command string
	var val string
	for true {
		fmt.Println("请输入命令")
		fmt.Scan(&command)

		switch command {

		case "add":
			fmt.Println("请输入你要添加的值")
			fmt.Scan(&val)
			index, _ := queue.Add(val)
			fmt.Println("index:", index, "val : ", val)
		case "take":
			data, _ := queue.Take()
			fmt.Println("data:", data)
		case "size":
			fmt.Println(queue.Size())
		case "show":

			queue.Show()

		}

	}

}


